﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Navigation;
using System.Management;
using System.Windows.Controls;

namespace Automator.Windows
{
    /// <summary>
    /// Interaction logic for AboutWindow.xaml
    /// </summary>
    public partial class AboutWindow : Window
    {
        public AboutWindow()
        {
            InitializeComponent();
        }
        
        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

        private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabControl.SelectedIndex == 0 && String.IsNullOrEmpty(edtMisc.Text))
            {
                //fill About Automator tab
                Assembly host = Assembly.GetExecutingAssembly();
                edtVersion.Text = host.GetName().Version.ToString();

                StringBuilder osVersion = new StringBuilder();
                var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
                            select x.GetPropertyValue("Caption")).First();
                osVersion.Append((name != null) ? name.ToString() : Environment.OSVersion.Platform.ToString());
                if (!String.IsNullOrEmpty(Environment.OSVersion.ServicePack))
                {
                    osVersion.Append(" ");
                    osVersion.Append(Environment.OSVersion.ServicePack);
                }

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(String.Format("Automator Version   : {0}", host.GetName().Version));
                sb.AppendLine(String.Format(".NET Version        : {0}", Environment.Version));
                sb.AppendLine(String.Format("Operating System    : {0}", osVersion.ToString()));
                sb.AppendLine(String.Format("OS Version          : {0}.{1}.{2}",
                                            Environment.OSVersion.Version.Major,
                                            Environment.OSVersion.Version.Minor,
                                            Environment.OSVersion.Version.Build));
                sb.AppendLine(String.Format("OS Architecture     : {0}", (Environment.Is64BitOperatingSystem) ? "x64" : "x86"));
                sb.AppendLine(String.Format("Current Culture     : {0} ({1})", CultureInfo.CurrentCulture.EnglishName, CultureInfo.CurrentCulture.ToString()));
                sb.AppendLine(String.Format("Current UI Language : {0}", Application.Current.MainWindow.Language.GetSpecificCulture().TwoLetterISOLanguageName));
                edtMisc.Text = sb.ToString();
            }
            else if (tabControl.SelectedIndex == 1 && gridVersions.ItemsSource == null)
            {
                //fill Version Info tab
                List<AssemblyName> assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies().ToList();
                assemblies.Sort(delegate(AssemblyName x, AssemblyName y) { return x.Name.CompareTo(y.Name); });
                var versions = assemblies.AsEnumerable()
                    .Select(a => new
                            {
                                Name = a.Name,
                                Version = a.Version.ToString(),
                                Path = Assembly.Load(a).Location
                            });
                gridVersions.ItemsSource = versions;
            }
        }
    }
}